package com.example.shiyansan.controller.user;

import com.example.shiyansan.entity.Cart;
import com.example.shiyansan.result.Result;
import com.example.shiyansan.service.CartService;
import com.example.shiyansan.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user/cart")
@RestController
public class CartController {

    @Autowired
    private CartService cartService;
    /**
     *添加图书
     */
    @PostMapping("/add/{bookId}")
    public Result add(@PathVariable int bookId, @RequestParam(defaultValue = "1") int quantity) throws Exception {
        cartService.add(bookId, quantity);
        return Result.success();
    }

    /**
     *修改图书数量
     */
    @PutMapping("/update")
    public Result update(@RequestBody Cart cart) throws Exception {
        cartService.update(cart);
        return Result.success();
    }

    /**
     * 删除图书
     */
    @DeleteMapping("/delete/{bookId}")
    public Result delete(@PathVariable int bookId) throws Exception {
        cartService.delete(bookId);
        return Result.success();
    }

    /**
     * 查看购物车列表
     */
    @GetMapping("/list")
    public Result<List<CartVo>> list(){
        List<CartVo> cartVoList = cartService.list();
        return Result.success(cartVoList);
    }

    /**
     * 结算购物车，生成订单
     */
    @PostMapping({"/insert", "/insert/{cartId}"})
    public Result submit(@PathVariable(required = false) Integer cartId) throws Exception {
        cartService.submit(cartId);
        return Result.success();
    }
}
